<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container">
    <nav class="tab">
      <a href="javascript:;" @click="hasAccount = true" :class="{active:hasAccount}">
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a href="javascript:;" @click="hasAccount = false" :class="{active:!hasAccount}">
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <!-- 有账号 -->
    <div class="tab-content" v-if="hasAccount">
      <CallbackBind :unionId="unionId" />
    </div>
    <!-- 没有账号 -->
    <div class="tab-content" v-else>
      <CallbackPatch :unionId="unionId" />
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import LoginFooter from './components/login-footer'
import LoginHeader from './components/login-header'
import CallbackBind from './components/callback-bind'
import CallbackPatch from './components/callback-patch'
import { onMounted, ref } from 'vue'
import { userQQLogin } from '@/api/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import QC from 'qc'
import Message from '@/components/Message/index.js'
export default {
  name: 'PageCallback',
  components: { LoginHeader, LoginFooter, CallbackBind, CallbackPatch },
  setup () {
    // 是否有账号
    const hasAccount = ref(true)
    const unionId = ref('')
    const store = useStore()
    const router = useRouter()
    onMounted(() => {
      // 检测是否已经进行qq登录(扫码或者是输入账号密码登录)
      if (QC.Login.check()) {
        // 使用qq提供的api获取openId
        QC.Login.getMe(async (openId) => {
          try {
            // console.log('openId', openId)
            unionId.value = openId
            // 调第三方登录接口
            const res = await userQQLogin(openId)
            // 把返回的用户信息保存到vuex中
            store.commit('user/setProfile', res.result)
            // 跳转页面到首页
            router.push('/')
            // 提示用户
            Message({ type: 'success', text: 'qq登录成功' })
          } catch (err) {
            // console.log(err)
            Message({ type: 'error', text: 'qq登录失败,请绑定账号' })
          }
        })
      }
    })
    return {
      hasAccount,
      unionId
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
